<!-- 
    节流: 按照固定的时间频率(间隔)来执⾏对应的函数
    应用场景:
        监听页面的滚动事件 通过节流来降低事件调用的频率
        ⿏标移动
        用户频繁点击按钮的操作
 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text">
    <script src="https://cdn.jsdelivr.net/npm/underscore@1.13.4/underscore-umd-min.js"></script>

    <script>
        // 自己实现节流函数
        function mlthrottle(fn, interval) {
            let startTime = 0;
            const _throttle = function () {
                let nowTime = new Date().getTime();
                let waitTime = interval - (nowTime - startTime)
                if (waitTime <= 0) {
                    fn();
                    startTime = nowTime;
                }
            }
            return _throttle;
        }
    </script>

    <script>
        // 获取input元素
        const inputEl = document.querySelector("input")

        // let counter = 1
        // inputEl.oninput = function () {
        //     console.log(`发送网络请求${counter++}:`, this.value)
        // };

        // underscore节流处理代码  throttle翻译就是节流
        // let counter = 1
        // inputEl.oninput = _.throttle(function () {
        //     console.log(`发送网络请求${counter++}:`, this.value)
        // }, 2000)

        // 自己实现的节流函数
        let counter = 1
        inputEl.oninput = mlthrottle(function () {
            console.log(`发送网络请求${counter++}:`)
        }, 2000);
    </script>
</body>

</html>